<template>
  <el-dialog
      title="绑定教室"
      :visible.sync="visible"
      :id.sync="id"
      :before-close="closeDialog"
      @opened="openDialog"
      width="400px">
    <el-form label-width="80px">
      <el-form-item label="教室" prop="device.classroomId" size="small">
        <el-select v-model="device.classroomId">
          <el-option
              v-for="item in classrooms"
              :key="item.id"
              :label="item.classroomName"
              :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item align="center" style="margin-left: -80px;">
        <el-button type="primary" size="small" plain @click="handleUpdate">修改设备</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {getDeviceById, updateDevice} from "@/api/device";
import {listClassrooms} from "@/api/classroom";

export default {
  name: 'DeviceBindDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      device: {},
      classrooms: [],
    }
  },
  methods: {
    // 更新设备信息
    handleUpdate() {
      console.log(this.device.classroomId)
      updateDevice({
        'classroomId': this.device.classroomId,
        'accessMode': this.device.accessMode,
        'createTime': this.device.createTime,
        'deviceCode': this.device.deviceCode,
        'deviceName': this.device.deviceName,
        'deviceType': this.device.deviceType,
        'entityId': this.device.entityId,
        'health': this.device.health,
        'id': this.device.id,
        'ip': this.device.ip,
        'locationCoding': this.device.locationCoding,
        'runtimeStatus': this.device.runtimeStatus,
        'username': this.device.username,

      }).then((response) => {
        if (response.data.code === 200) {
          this.$parent.fetchData()
          this.$alert('提交成功', '提示', {
            confirmButtonText: '确定'
          })

        } else {
          this.$alert('提交失败', '提示', {
            confirmButtonText: '确定'
          })
        }
      })
      this.$emit('update:visible', false)
    },

    // 打开对话框
    openDialog() {
      listClassrooms(1, 20).then((response) => {
        this.classrooms = response.data.data.list;
        console.log(this.classrooms);
      })
      getDeviceById(this.id).then((response) => {
        this.device = response.data.data;
      })
      console.log(this.device)

      console.log(this.classrooms)
    },

    // 关闭对话框
    closeDialog() {
      this.$emit('update:visible', false)
    },
  }
}
</script>

<style scoped>

</style>
